package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.req.ConfirmPickupInStoresReq;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.ConfirmPickupInStoresCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.sharding.db.sharding.model.DeliveryBizPo;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sharding.model.OrderStatusLogPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_SqConfirmPickupInStores {

	private static ConfirmPickupInStoresReq confirmPickupInStoresReq;
	private static ConfirmPickupInStoresCall confirmPickupInStoresCall;
	
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	private static long japanGoodsOwnerId = ShenqiUserSet.japanGoodsOwnerId;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("货头确认提货");
	}

	@Before
	public void caseUp() {
		confirmPickupInStoresReq = new ConfirmPickupInStoresReq();
		confirmPickupInStoresCall = new ConfirmPickupInStoresCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(confirmPickupInStoresCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	/**
	 * 校验订单表状态，订单主状态操作日志表， 订单业务日志表
	 */
	public static void verifyDb(long orderId) {

		// 订单表(order)
		OrderPo ymtOrders = orderShardingWapper.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));

		Logger.verifyEquals(3, ymtOrders.getOrderStatus(), "验证orderStatus");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(ymtOrders.getDeliveryTime()) < 2,
				"验证数据库deliveryTime跟系统当前时间差为2分钟内");

		//订单主状态操作日志表(order_status_log)
		OrderStatusLogPo orderStatusLogPo = orderShardingWapper
				.selectOrderStatusLogByOrderId(orderId);

		Logger.comment("验证订单主状态操作日志表(order_status_log)");
	
		Logger.verifyEquals("确认提货，客服:" + confirmPickupInStoresReq.getCsId(), orderStatusLogPo
				.getMemo(), "验证数据库Memo");
		Logger.verifyEquals(3, orderStatusLogPo.getOrderStatus(),
				"验证数据库OrderStatus");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(orderStatusLogPo.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, orderStatusLogPo.getOrderId(),
				"验证数据库OrderId");
		Logger.verifyEquals(confirmPickupInStoresReq.getGoodsOwnerId(),
				orderStatusLogPo.getOperator(), "验证数据库Operator");

		// 订单业务日志表(order_biz_log)
		OrderBizLogPo orderBizLogPo = orderShardingWapper
				.selectOrderBizLogByOrderId(orderId);
		
		Logger.comment("验证订单业务日志表(order_biz_log)");
		
		Logger.verifyEquals("确认提货，客服:" + confirmPickupInStoresReq.getCsId(),
				orderBizLogPo.getContent(), "验证数据库Content");
		Logger.verifyEquals("DELIVERY", orderBizLogPo.getBizType(),
				"验证数据库BizType");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(orderBizLogPo.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(),
				"验证数据库OrderId");
		Logger.verifyEquals(orderId, Long.valueOf(orderBizLogPo.getBizNo()),
				"验证数据库BizNo");
		Logger.verifyEquals(confirmPickupInStoresReq.getGoodsOwnerId(),
				orderBizLogPo.getOperator(), "验证数据库Operator");
		
	}
	
	private void verifyDeliveryBiz(long orderId, int vStockStatus, int deliveryStatus) {
		
		Logger.comment("校验deliveryBiz表：" + orderId);
		
		DeliveryBizPo deliveryBizPo = orderShardingWapper.selectDeliveryBizByOrderId(orderId);
		
		Logger.verifyEquals(vStockStatus, deliveryBizPo.getvStockStatus(),
				"验证vStockStatus");
		Logger.verifyEquals(deliveryStatus, deliveryBizPo.getdeliveryStatus(),
				"验证deliveryStatus");
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	// 代理订单不能上门自提
	public void Tc_ConfirmPickupInStores_001() {
		Logger.start(true, "创建一个自提订单，客服确认自提成功");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithSelfPickup(productBean,
							autotest_goodsOwnerId, true);
			
			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = resp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			confirmPickupInStoresReq.setOrderId(orderId);
			confirmPickupInStoresReq.setGoodsOwnerId(autotest_goodsOwnerId);
			confirmPickupInStoresReq.setCsId(123456L);
			
			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();
			
			Logger.verifyEquals(true, confirmPickupInStoresCall.getSuccess(),
					"验证success");

			verifyDb(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmPickupInStores_002() {
		Logger.start(true, "创建一个自提订单，货头确认自提成功");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithSelfPickup(productBean,
							autotest_goodsOwnerId, true);
			
			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = resp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			confirmPickupInStoresReq.setOrderId(orderId);
			confirmPickupInStoresReq.setGoodsOwnerId(autotest_goodsOwnerId);
			
			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();
			
			Logger.verifyEquals(true, confirmPickupInStoresCall.getSuccess(),
					"验证success");

			verifyDb(orderId);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmPickupInStores_006() {
		Logger.start(true, "非自提订单，确认自提失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = resp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			confirmPickupInStoresReq.setOrderId(orderId);
			confirmPickupInStoresReq.setGoodsOwnerId(autotest_goodsOwnerId);
			
			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();

			Logger.verifyEquals(false, confirmPickupInStoresCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(102, confirmPickupInStoresCall.getCode(),
					"验证errorCodeValue");
			Logger.verifyEquals(String.format("%s订单不是自提方式，无法进行此操作", orderId),
					confirmPickupInStoresCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmPickupInStores_007() {
		Logger.start(true, "pickupInStores为false的自提订单，确认自提失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithSelfPickup(productBean,
							autotest_goodsOwnerId, false);

			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = resp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");
			
			confirmPickupInStoresReq.setOrderId(orderId);
			confirmPickupInStoresReq.setGoodsOwnerId(autotest_goodsOwnerId);
			
			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();

			Logger.verifyEquals(false, confirmPickupInStoresCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(102, confirmPickupInStoresCall.getCode(),
					"验证errorCodeValue");
			Logger.verifyEquals(String.format("%s订单不是自提方式，无法进行此操作", orderId),
					confirmPickupInStoresCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmPickupInStores_008() {
		Logger.start(true, "创建一个自提订单，货头二次确认自提失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithSelfPickup(productBean,
							autotest_goodsOwnerId, true);
			
			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = resp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			confirmPickupInStoresReq.setOrderId(orderId);
			confirmPickupInStoresReq.setGoodsOwnerId(autotest_goodsOwnerId);
			
			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();
			
			//二次确认
			confirmPickupInStoresCall.callService();
			
			Logger.verifyEquals(false, confirmPickupInStoresCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(106, confirmPickupInStoresCall.getCode(),
					"验证errorCodeValue");
			Logger.verifyEquals(String.format("%s订单已提货，无需再次确认提货", orderId),
					confirmPickupInStoresCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmPickupInStores_009() {
		Logger.start(true, "创建一个自提订单，orderId为空");
		try {

			// confirmPickupInStoresReq.setOrderId(orderId);
			confirmPickupInStoresReq.setGoodsOwnerId(autotest_goodsOwnerId);

			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();

			Logger.verifyEquals(false, confirmPickupInStoresCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(100, confirmPickupInStoresCall.getCode(),
					"验证errorCodeValue");
			Logger.verifyEquals("orderId:订单号不能为空",
					confirmPickupInStoresCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ConfirmPickupInStores_010() {
		Logger.start(true, "创建一个自提订单，goodsOwnerId为空");
		try {

			confirmPickupInStoresReq.setOrderId(1);
			confirmPickupInStoresReq.setGoodsOwnerId(0);

			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();

			Logger.verifyEquals(false, confirmPickupInStoresCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(100, confirmPickupInStoresCall.getCode(),
					"验证errorCodeValue");
			Logger.verifyEquals("goodsOwnerId:买手id不能为空",
					confirmPickupInStoresCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	public void Tc_ConfirmPickupInStores_011() {
		Logger.start(true, "创建一个包含虚拟库存的自提订单，实际库存充足，货头确认自提成功");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithSelfPickup(productBean,
							autotest_goodsOwnerId, true, 2);
			
			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = resp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			// 添加实际库存
			DGSQBuildProxyChainCallService.modifyGoodsOwnerProdStock(
					(int) autotest_goodsOwnerId, productBean, 0, 10);
			
			confirmPickupInStoresReq.setOrderId(orderId);
			confirmPickupInStoresReq.setGoodsOwnerId(autotest_goodsOwnerId);
			
			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();
			
			Logger.verifyEquals(true, confirmPickupInStoresCall.getSuccess(),
					"验证success");

			verifyDb(orderId);
			
			// 校验虚拟库存发货表delivery_biz
			verifyDeliveryBiz(orderId, 2, 2);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	public void Tc_ConfirmPickupInStores_012() {
		Logger.start(true, "创建一个包含虚拟库存的自提订单，实际库存不足，货头确认自提失败");
		try {
			
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);

			TradingServiceResp resp = SqTradingCallService
					.placeOrderWithSelfPickup(productBean,
							autotest_goodsOwnerId, true, 2);
			
			Long userId = resp.getUserId();
			long orderId = resp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = resp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					resp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(5000);
			
			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			
			Logger.comment("前置数据准备结束");
			
			confirmPickupInStoresReq.setOrderId(orderId);
			confirmPickupInStoresReq.setGoodsOwnerId(autotest_goodsOwnerId);
			
			confirmPickupInStoresCall.setData(confirmPickupInStoresReq);
			confirmPickupInStoresCall.callService();
			
			Logger.verifyEquals(false, confirmPickupInStoresCall.getSuccess(),
					"验证success");
			Logger.verifyEquals(108, confirmPickupInStoresCall.getCode(),
					"验证errorCodeValue");
			Logger.verifyEquals("商品库存不足，无法发货，请先入库商品真实库存",
					confirmPickupInStoresCall.getMsg(), "验证errorMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
